﻿using System;
using System.Web.UI.WebControls;
using AB.ServiceDirectory;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.DepartmentsMaintenance
{
    public partial class DepartmentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var filter = new DepartmentFilter();
            ViewState["filter"] = filter;
            ViewState["sortingButton"] = LinkButtonAll.UniqueID;
        }

        protected void ObjectDataSourceDepartmentList_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            e.InputParameters["filter"] = filter;
        }

        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            if (filter !=null)
            {
                var beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);
                var letterGroupId = ViewState["sortingButton"] as string;
                var letterGroupButton = FindControl(letterGroupId) as LinkButton;
                if (letterGroupButton !=null)
                {
                    var clickedButton = sender as LinkButton;
                    letterGroupButton.CssClass = string.Empty;
                    clickedButton.CssClass = "hg-blue";
                    ViewState["sortingButton"] = clickedButton.UniqueID;
                }
                GridViewDepartmentList.PageIndex = 0;
                GridViewDepartmentList.DataBind();
            }
        }

        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as DepartmentFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            this.GridViewDepartmentList.DataBind();
        }

        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
           Response.Redirect("~/DepartmentsMaintenance/DepartmentDetails.aspx");
        }
    }
}